import {Position} from "@/components/comm/utils";

export class Block {
    constructor(
        public center: Position,
        public children: Position[],
        public color: string,
    ) {
    }

    public clone(): Block {
        return new Block(
            this.center.slice() as Position,
            this.children.map(a => a.slice() as Position),
            this.color,
        )
    }

    public contains(pos: Position): boolean {
        return this.children.some(p => p.equals(pos));
    }

    public move(dx: number, dy: number): void {
        this.center[0] += dx;
        this.center[1] += dy;
        for (const p of this.children) {
            p[0] += dx;
            p[1] += dy;
        }
    }

    public moveTo(cx: number, cy: number): void {
        const dx = cx - this.center[0];
        const dy = cy - this.center[1];
        this.move(dx, dy);
    }

    public rotateLeft(): void {
        const [x2, y2] = this.center;
        for (const [i, [x1, y1]] of this.children.entries()) {
            this.children[i] = [y1 - y2 + x2, x2 - x1 + y2];
        }
    }

    public rotateRight(): void {
        const [x2, y2] = this.center;
        for (const [i, [x1, y1]] of this.children.entries()) {
            this.children[i] = [y2 - y1 + x2, x1 - x2 + y2];
        }
    }

}